//
//  Fraction.m
//  02-ClassAndObject
//
//  Created by dllo on 16/7/5.
//  Copyright © 2016年 高雅馨. All rights reserved.
//

#import "Fraction.h"

@implementation Fraction
- (instancetype)initWithMemeber:(NSInteger)member Deno:(NSInteger)deno {
    self = [super init];
    if (self) {
        _member = member;
        _deno = deno;
    }
    return self;
}
+ (instancetype)fractionWithMemeber:(NSInteger)member Deno:(NSInteger)deno {
    return  [[[Fraction  alloc] initWithMemeber:member Deno:deno] autorelease];
}

- (Fraction *)addAnotherFraction:(Fraction *)anotherFraction {
    NSInteger newMember = self.member * anotherFraction.deno + self.deno * anotherFraction.member;
    NSInteger newDeno = self.deno * anotherFraction.deno;
    Fraction *resultAdd =[Fraction fractionWithMemeber:newMember Deno:newDeno];
    [resultAdd reduction];
    [resultAdd outPut];
    return resultAdd;
    
};

- (Fraction *)subAnotherFraction:(Fraction *)anotherFraction {
    NSInteger newMember = self.member * anotherFraction.deno - self.deno * anotherFraction.member;
    NSInteger newDeno = self.deno * anotherFraction.deno;
    Fraction *resultSub = [Fraction fractionWithMemeber:newMember Deno:newDeno];
    [resultSub reduction];
    [resultSub outPut];
    return resultSub;
}

- (Fraction *)mulAnotherFraction:(Fraction *)anotherFraction {
    NSInteger newMember = self.member * anotherFraction.member;
    NSInteger newDeno = self.deno * anotherFraction.deno;
    Fraction *resultMul = [Fraction fractionWithMemeber:newMember Deno:newDeno];
    [resultMul reduction];
    [resultMul outPut];
    return resultMul;
}

- (Fraction *)divAnotherFraction:(Fraction *)anotherFraction {
    NSInteger newMember = self.member * anotherFraction.deno;
    NSInteger newDeno = self.deno * anotherFraction.member;
    Fraction *resultDiv = [Fraction fractionWithMemeber:newMember Deno:newDeno];
    [resultDiv reduction];
    [resultDiv outPut];
    return resultDiv;
}
/**
 *  最大公约数
 */
- (NSInteger)maxGYSwithNum1:(NSInteger)num1
                                           Num2:(NSInteger)num2 {
    NSInteger min = num1 < num2 ? num1 : num2;
    NSInteger maxGYS = 1;
    // 求绝对值 abs(int)
    int result = abs((int)min);
    for (NSInteger i = result ; i > 0; i--) {
        if (num1 % i == 0 && num2 % i == 0) {
            maxGYS = i;
            break;
        }
    }
    return maxGYS;
}
// 约分
- (void)reduction {
    NSInteger max = [self maxGYSwithNum1:self.member Num2:self.deno];
    self.member /= max;
    self.deno /=  max;
}
/**
 *  输出结果
 */
- (void)outPut {
    if (self.member == self.deno && self.deno != 0) {
        NSLog(@"结果为1");
    } else if (self.member + self.deno == 0) {
        NSLog(@"结果为-1");
    } else if(self.deno == 0) {
        NSLog(@"error");
    } else if (self.member == 0) {
        NSLog(@"结果为0");
    } else {
        NSLog(@"结果为%ld/%ld",self.member, self.deno);
    }
}
/**
 *  打印的不是地址是对象名
 */
- (NSString *)description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          {
    return [NSString stringWithFormat:@"<%ld-%ld>", self.member, self.deno];
}
@end
